package com.atcjx.controller;

import com.atcjx.MyUtils.ResultFactory;
import com.atcjx.exception.UserException;
import com.atcjx.exception.exceptionEnum;
import com.atcjx.pojo.Article.ArticleDTO;
import com.atcjx.pojo.Article.ArticleVO;
import com.atcjx.pojo.Query.Article.ArticleQueryDTO;
import com.atcjx.pojo.Result;
import com.atcjx.service.ArticleServiceImpl;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.naming.Name;
import javax.validation.Valid;

/**
 * @author admin
 * @version 1.0.0
 * @ClassName ArticleController.java
 * @Description TODO
 * @createTime 2021年09月20日 16:27:00
 */
@Api("文章的相关接口")
@RestController
public class ArticleController {
    @Autowired
    private ArticleServiceImpl articleService;

    @ApiOperation("批量获取文章接口")
    @GetMapping("/get")
    public Result getArticles(
             @ApiParam("前端传入的查询条件 如果是多个条件请用','分隔开")
             ArticleQueryDTO articleQueryDTO,
             @ApiParam("页码")
             @RequestParam(required = false, defaultValue = "1") Integer page,
             @ApiParam("每页存放的记录数")
             @RequestParam(required = false, defaultValue = "5") Integer limit )
    {
        return ResultFactory.successWithData(articleService.getArticles(articleQueryDTO,page,limit));
    }

    @PostMapping("/articles")
    @ApiOperation("新增一篇文章接口")
    public Result postArticles(@ApiParam(name = "文章信息", value = "传入的JSON格式", required = true)
                               @RequestBody @Valid ArticleDTO articleDTO) {
        return ResultFactory.successWithData(articleService.insArticle(articleDTO));
    }

    @DeleteMapping("article/{id}")
    @ApiOperation("通过id号删除文章")
    @ApiImplicitParam(name = "id", value = "文章的id号", required = true, dataType = "String", paramType = "path")
    public Result deleteArticle(@PathVariable String id) {
        articleService.deleteById(id);
        return ResultFactory.success("删除成功!");
    }

    @PutMapping("article/{id}")
    @ApiOperation("修改文章")
    @ApiImplicitParam(name = "id", value = "文章的id号", required = true, dataType = "String", paramType = "path")
    public Result updateArticle(
            @ApiParam(name = "需要修改的文章信息",value = "传入json格式的数据",required = true)
            @RequestBody ArticleDTO articleDTO,
            @PathVariable String id){
        articleService.updateArticle(articleDTO,id);
        return ResultFactory.success("更新成功！");
    }
}
